const path = require('path')
const {smart} = require('webpack-merge')
const base = require('./webpack.base.js')
const Webpack = require('webpack')
const OptimizeCssPlugin = require('optimize-css-assets-webpack-plugin')
const DATA = require("./data.js")

module.exports = smart(base,{
	mode:'production',
	
	/* 出口文件 */
	output: {
		filename: 'js/[name].[hash:8].js',
		chunkFilename:'js/[name].[hash:8].js',
		path: path.resolve(__dirname, 'dist'),
		//publicPath:'',
	},
	
	/* 实时打包 */
	watch:true,	
	watchOptions:{
		poll:1000,	//每秒询问1000次
		aggregateTimeout:500,	//超时500毫秒
		ignored:/node_modules/	//忽略module文件夹
	},
	
	/* 优化项 */
	optimization:{
		splitChunks: {	//分割代码块
			chunks: "all",
			minSize: 30000, // 模块的最小体积
			minChunks: 1, // 模块的最小被引用次数
			maxAsyncRequests: 5, // 按需加载的最大并行请求数
			maxInitialRequests: 3, // 一个入口最大并行请求数
			automaticNameDelimiter: '~', // 文件名的连接符
			name: true,
			cacheGroups: {
				commons: {
					name: "commons",	//chunk名
					chunks: "initial",	//从入口处开始抽离
					minChunks: 2
				},
				vendor:{
					priority:1,	//权重1
					test: /node_modules/,
					name: "vendor",
					chunks: "all",
					minChunks: 2
				}
			}
		},
		runtimeChunk: {	//会给每个入口添加一个只包含runtime的额外的代码块,需要在HtmlWebpackPlugin chunk
			name:'split'
		}
	},
	
	plugins:[
		/* 定义环境变量 */
		new Webpack.DefinePlugin({
			GLOBAL:{
				host:JSON.stringify(DATA.proHost),	//域
			}
		}),
		
		/* css压缩 */
		new OptimizeCssPlugin(),
		
		/* 版权信息 */
		new Webpack.BannerPlugin('@author HuiSir版权所有;@introduce 智能眼科学会'),
	]
})